using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NET.Library.Web.Forms.DataControls.DataLists
{
    /// <summary>
    /// Populate variable types with values from DataList control data items.
    /// </summary>
    public class DataListDataItems
    {
        /// <summary>
        /// Return DataList control data item as a string type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static string StringDataItem(string fieldName, DataListItemEventArgs e)
    {
        return DataBinder.Eval(e.Item.DataItem, fieldName).ToString();
    }

        /// <summary>
    /// Return DataList control data item as a DateTime type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static DateTime DateTimeDataItem(string fieldName, DataListItemEventArgs e)
        {
            return (DateTime)DataBinder.Eval(e.Item.DataItem, fieldName);
        }

        /// <summary>
        /// Return DataList control data item as an integer type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static int IntDataItem(string fieldName, DataListItemEventArgs e)
        {
            return (int)DataBinder.Eval(e.Item.DataItem, fieldName);
        }

        /// <summary>
        /// Return DataList control data item used as an Image type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static byte[] ImageDataItem(string fieldName, DataListItemEventArgs e)
        {
            return (byte[])DataBinder.Eval(e.Item.DataItem, fieldName);
        }

        /// <summary>
        /// Return DataList control data item used as a Boolean type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static Boolean BoolDataItem(string fieldName, DataListItemEventArgs e)
        {
            string obj = DataBinder.Eval(e.Item.DataItem, fieldName).ToString();
            if (obj == "1")
                return true;
            else
                return false;
            //return obj == 1 ? true : false;
            //return (bool)DataBinder.Eval(e.Item.DataItem, fieldName);
            //return boolValue(DataBinder.Eval(e.Item.DataItem, fieldName));
        }

        private static bool boolValue(object Object)
        {
            return Object == DBNull.Value ? false : Convert.ToBoolean(Object);
        }
    }
}
